home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / k_r.arc / K&R_CH2.C < prev    next >
Text File  |  1985-11-05  |  3KB  |  201 lines

  1. pr 2.*
  2.  
  3.  
  4. Jul 27 17:12 1984  2.34conv.c Page 1
  5.  
  6.  
  7. /* type conversion */
  8.  
  9. main()  /* print conversion table from cm to inches
  10.         /* 1 inch = 2.54 cm - from 1 through 12 cm */
  11. {
  12.         int cm;
  13.  
  14.         for (cm = 1; cm <= 12; ++cm)
  15.           printf("cm = %3d, inch = %6.2f\n", cm, cm / 2.54);
  16. }
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Jul 27 17:12 1984  2.50comma.c Page 1
  71.  
  72.  
  73. main()  /* calculate the sum of all integers between and including
  74.         /* 2 input integers, compute their average and remainder */
  75. { int   i, n1, n2, ct, sm;
  76.   n1 = getint(); n2 = getint();
  77.   if (n1 <= n2)
  78.   { for (ct = sm = 0, i = n1; i <= n2; ++i)
  79.     {   sm += i; ++ct;
  80.     }
  81.     printf("n1 = %d, n2 = %d, sum = %d, ave = %.2f, remainder = %d\n",
  82.                 n1, n2, sm, (float) sm / ct, sm % ct);
  83.   }
  84.   else
  85.   { printf("2nd number not big enough\n"); exit(1);
  86.   }
  87. }
  88. getint()        /* function to convert ASCII
  89.                 /* input from terminal to an int */
  90. {       int     c, sign = 1, num = 0;
  91.  
  92.         printf("input integer: ");
  93.         if      ((c = getchar()) == '-')
  94.                 sign = -1;
  95.         else if (c >= '0' && c <= '9')
  96.                 num = (c - '0');
  97.         else
  98.                 return (0);
  99.         while ((c = getchar()) >= '0' && c <= '9')
  100.                 num = num * 10 + (c - '0');
  101.         return (num * sign);
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Jul 27 17:12 1984  2.58itob.c Page 1
  137.  
  138.  
  139. #define SZ      8 * sizeof(int) /* for portabilty       */
  140. main()  /* function to test itob(), integer to binary */
  141. {       char asc[SZ+1];
  142.         int i = getint();
  143.         itob(i, asc);
  144.         printf("dec = %d, octal = %o, binary = %s\n", i, i, asc);
  145. }
  146. itob(num, ar)   /* convert int to ASCII 0's and 1's */
  147. int num;        /* received int */
  148. char ar[];      /* array to store 0's and 1's */
  149. {       int cnt, mask = 1;
  150.         for (cnt = SZ - 1; cnt >= 0; --cnt)
  151.         {       ar[cnt] = ((num & mask)? '1': '0'); mask <<= 1;
  152.         }
  153.         ar[SZ] = '\0';
  154. }
  155. getint()        /* function to convert ASCII
  156.                 /* input from terminal to an int */
  157. {       int     c, sign = 1, num = 0;
  158.  
  159.         printf("input integer: ");
  160.         if      ((c = getchar()) == '-')
  161.                 sign = -1;
  162.         else if (c >= '0' && c <= '9')
  163.                 num = (c - '0');
  164.         else
  165.                 return (0);
  166.         while ((c = getchar()) >= '0' && c <= '9')
  167.                 num = num * 10 + (c - '0');
  168.         return (num * sign);
  169. }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.